package com.example.jinggai;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.json.JSONException;
import org.json.JSONObject;

public class BaojingActivity extends AppCompatActivity {



    private static MqttClient mqttClient;
    private static MqttConnectOptions mqttConnectOptions;

    EditText height,nongdu,jijaodu;//高度，浓度，角度
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_baojing);
        height = findViewById(R.id.et_hieght);
        nongdu = findViewById(R.id.et_nondu);
        jijaodu = findViewById(R.id.et_jiaodu);
        initMqttClient();
    }


    private void initMqttClient() {
        mqttConnectOptions  = new MqttConnectOptions();//MQTT的连接设置
        mqttConnectOptions.setCleanSession(true);//清除连接信息

        mqttConnectOptions.setConnectionTimeout(3);// 设置连接超时时间 单位为秒
        mqttConnectOptions.setKeepAliveInterval(60);//心跳包时间60S
        try { mqttClient = new MqttClient(
                "tcp://114.96.74.180:1883",//连接的地址信息
                "android",//ClientID,使用当前时间戳
                new MemoryPersistence());//
            mqttClient.setCallback(new MqttCallback() {//回调函数
                @Override//连接断开
                public void connectionLost(Throwable throwable) { }
                @Override//接收到消息
                public void messageArrived(String s, MqttMessage mqttMessage)  {
                    Log.e("MqttMsg:", "Topic:"+s+" "+"Msg:"+mqttMessage.toString());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            String source = mqttMessage.toString();

                            try {
                                JSONObject obj = new JSONObject(source);
                                if(source.contains("Height")){  //高度


                                    JSONObject pa = obj.getJSONObject("params");

                                    if(pa.getInt("Height") < 30){

                                        height.setText("高度报警"+pa.getInt("Height"));
                                    }else {

                                        height.setText("正常"+pa.getInt("Height"));
                                    }


                                }
                                if(source.contains("GasConcentration")){  //气体浓度


                                    JSONObject pa = obj.getJSONObject("params");

                                    if(pa.getInt("GasConcentration") > 30){

                                        nongdu.setText("气体浓度报警"+pa.getInt("GasConcentration"));
                                    }else {

                                        nongdu.setText("正常"+pa.getInt("GasConcentration"));
                                    }


                                }
                                if(source.contains("Key")){  //角度

                                    JSONObject pa = obj.getJSONObject("params");

                                    if(pa.getInt("Key") > 30){

                                        jijaodu.setText("角度报警"+pa.getInt("Key"));
                                    }


                                }


                            } catch (JSONException e) {
                                e.printStackTrace();
                            }


                        }
                    });
                }
                @Override//没用过
                public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { }
            });
            new Thread(new Runnable() {//用任务
                @Override
                public void run() {
                    try {
                        mqttClient.connect(mqttConnectOptions);
                        if (mqttClient.isConnected()) {
                            Log.e("Mqtt Info", "ConnectSuccess");
                            mqttClient.subscribe("/sys/k0m4uijls9a/demo1/thing/event/property/post",0);//订阅1111,消息等级0
                        }
                    }catch (Exception e){
                        Log.e("Mqtt Err", e.toString());
                    }
                }
            }).start();
        }
        catch (Exception e){
            Log.e("Mqtt Err", e.toString());
        }


    }
}